"""用户信息表"""
import json
from sqlalchemy import Column, String, Text
from src.models.db import db


class InvData(db.Models):
    """发票信息表"""
    __tablename__ = 'inv_data'
    id = Column(String(100), primary_key=True, unique=True)
    inv_code = Column(String(100))
    inv_num = Column(String(100))
    inv_date = Column(String(100))
    inv_chk = Column(String(100))
    inv_state = Column(String(100))
    inv_details = Column(Text, default='')

    def save_inv_details(self, inv_details: dict):
        if not inv_details or not isinstance(inv_details, dict):
            return False, 'Not valid inv_data'
        try:
            inv_details_str = json.dumps(inv_details)
            with db.Session() as sess:
                self.inv_details = inv_details_str
                sess.add(self)
                sess.commit()
        except Exception as e:
            return False, e

    def get_inv_details(self) -> dict:
        return json.loads(self.inv_details) if self.inv_details else {}

    def __repr__(self):
        return f'<Inv {self.inv_code}-{self.inv_num}>'


db.init_db()
